package com.springcloud.sc.ucpv5.service.impl;

import com.springcloud.sc.ucp.v5.api.mongo.locationdb.RoleInfo;
import com.springcloud.sc.ucp.v5.api.vo.RoleInfoQueryReq;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.service.UcpRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class UcpRoleServiceImpl implements UcpRoleService {

    @Autowired
    @Qualifier(value = "locationmongoTemplate")
    private MongoTemplate locationmongoTemplate;

/*    @Autowired
    @Qualifier(value = "ucpMongoTemplate")
    private MongoTemplate ucpMongoTemplate;*/

    public List getRoleDataInfo(RoleInfoQueryReq roleInfoQueryReq) {
        Criteria criteria  = null;
        if (roleInfoQueryReq.type == 0) {
            criteria  = new  Criteria().where("nodeNumber").is(roleInfoQueryReq.nodeNumber);
        } else if (roleInfoQueryReq.type == 1) {
            criteria  = new Criteria().where("nodeNumber").is(roleInfoQueryReq.nodeNumber).and("type").is(0);
        } else if (roleInfoQueryReq.type == 2) {
            criteria  = new Criteria().where("userparamlist.number").is(roleInfoQueryReq.userNumber);
        } else if (roleInfoQueryReq.type == 3) {
            // 难点的where条件设置
            criteria  = new Criteria().where("_id").is(roleInfoQueryReq.roleId);
        }
        Query query = new Query( criteria ) ;
        List<RoleInfo> list =  locationmongoTemplate.find( query , RoleInfo.class );

        if (list.size() == 0 && roleInfoQueryReq.type == 2) {
            // 如果根据用户号码没有找到指定的角色, 策略 1.寻找当前节点的默认调度权限 2.遍历寻找父根节点的默认调度权限(迭代)
            return getRoleDataInfoByNodeNumber(roleInfoQueryReq.nodeNumber);

        }
        return list;
    }

    /**
     * 迭代往上查询节点的默认调度角色
     *
     * @param nodeNumber
     * @return
     */
    public List<RoleInfo> getRoleDataInfoByNodeNumber(String nodeNumber) {
        Query query = new Query(new Criteria().where("nodeNumber").is(nodeNumber).and("type").is(1));
        List<RoleInfo> resultList = locationmongoTemplate.find(query, RoleInfo.class);
        if (resultList.size() == 0) {
            MyNode node = BaseDataManager.getInstance().nodeGet(nodeNumber);
            if (node != null) {
                if (node.getIdlData().ParentNumber == null || node.getIdlData().ParentNumber.equals("")) {
                    return null;
                } else {
                    return getRoleDataInfoByNodeNumber(node.getIdlData().ParentNumber);
                }
            } else {
                String pattern = String.format("^.{%s,}$", 3);
                Criteria criteria = Criteria.where("nodeNumber").regex(pattern, "m").and("type").is(1);

                query = new Query(criteria);
                resultList = locationmongoTemplate.find(query, RoleInfo.class);
                return resultList;
            }
        } else {
            return resultList;
        }
    }



    @Override
    public RoleInfo updateRole(RoleInfo roleInfo) {
        locationmongoTemplate.save( roleInfo );
        return roleInfo;
    }

    @Override
    public RoleInfo saveRole(RoleInfo roleInfo) {
        locationmongoTemplate.save( roleInfo );
        return roleInfo;
    }

    @Override
    public RoleInfo deleteRole(RoleInfo roleInfo) {
        locationmongoTemplate.remove( roleInfo );
        return roleInfo;
    }
}
